package org.dromara.system.service.impl.trading.channel;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.system.config.core.RedisDelayMQSender;
import org.dromara.system.converter.MachineConverters;
import org.dromara.system.domain.TradingRecords;
import org.dromara.system.domain.bo.mxj.MXJBo;
import org.dromara.system.domain.bo.mxj.MXJTradingWaterBo;
import org.dromara.system.domain.vo.mxj.MXJVo;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.RedisMQConstant;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service("mXJTradingService")
@RequiredArgsConstructor
public class MXJTradingService extends AbstractTradingService<Object, MXJTradingWaterBo> {

    @Resource
    private RedisDelayMQSender redisDelayMQSender;


    @Override
    public NotificationEnum getNotificationEnum() {
        return NotificationEnum.MXJ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void tradingWaterConvert(MXJTradingWaterBo reqDTO) {
        TradingRecords tradingRecords = MachineConverters.INSTANCE.mXJlRecordsToReceiveDealRecords(reqDTO);
        Date date = DateUtil.parse(reqDTO.getTxnTm(), "yyyyMMddHHmmss");
        tradingRecords.setTradingTime(date);
        R<TradingRecords> tradingRecordsR = dealUserTrading(tradingRecords);
    }

    @Override
    public Object tradingWaterAssemble(String mapBody) {
        log.info("梦想家交易参数={}", JSONUtil.toJsonStr(mapBody));
        MXJBo mxjBo = JSONUtil.toBean(mapBody, MXJBo.class);
        MXJTradingWaterBo mxjTradingWaterBo = JSONUtil.toBean(mxjBo.getData(), MXJTradingWaterBo.class);
        if (StrUtil.equals(mxjTradingWaterBo.getBusType(), "Tran")) {
            String depositAmt = mxjTradingWaterBo.getDepositAmt();
            if (StrUtil.isNotBlank(depositAmt)) {
                BigDecimal decimal = Convert.toBigDecimal(depositAmt);
                updateMachineDepositAmt(mxjTradingWaterBo.getSnNo(), decimal);
            }
            redisDelayMQSender.send(RedisMQConstant.TRADING_QUEUE, JSONUtil.toJsonStr(mxjTradingWaterBo), getNotificationEnum(), mxjTradingWaterBo.getOrdNo());

        }
        return new MXJVo();
    }
}
